﻿using System;
using System.Data;
using TrafficArchives.Client;
using TrafficArchives.DataAccess;
using TrafficArchives.Logs;
using TrafficArchives.Models;
using TrafficArchives.Utilities;

namespace TrafficArchives.Archives
{
    /// <summary>
    /// 新增信誉档案。
    /// </summary>
    public partial class AddArchives : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initilize();
            }
        }

        private void Initilize()
        {
            var action = Request.QueryString["action"];
            var id = Request.QueryString["id"];
            LblTitle.Text = "添加信誉档案";
            TableQuery.Visible = string.IsNullOrEmpty(action); // 只有新增才显示。
            bool isDisplayData = false;

            var archivesTypeData = new ValueObjectDb().GetDataByClassCode(AppSettings.信誉种类编码);
            DdlArchiveType.DataSource = archivesTypeData;
            DdlArchiveType.DataTextField = "Name";
            DdlArchiveType.DataValueField = "Code";
            DdlArchiveType.DataBind();

            if (string.IsNullOrEmpty(action))
            {
                LblManageOrgan.Text = LoginUser.Current.DepartmentName;
                LblManageOrganID.Value = LoginUser.Current.DepartmentId;
                TxtRecordTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                DivSave.Visible = DivClear.Visible = true;
                DivBack.Visible = false;
                if (archivesTypeData.Rows.Count > 0)
                {
                    DdlArchiveType.SelectedIndex = 0;
                }
            }
            else if (action == "look")
            {
                LblTitle.Text = "查看信誉档案";
                DivSave.Visible = DivClear.Visible = false;
                DivBack.Visible = true;
                TxtContent.Enabled = false;
                TxtRecordTime.Enabled = false;
                DdlArchiveType.Enabled = false;
                isDisplayData = true;
            }
            else if (action == "edit")
            {
                LblTitle.Text = "修改信誉档案";
                DivSave.Visible = true;
                DivBack.Visible = true;
                DivClear.Visible = false;
                isDisplayData = true;
            }
            if (isDisplayData)
            {
                var db = new ArchivesDb();
                var data = db.GetData(id);
                DisplayArchives(data);
            }
        }

        private void DisplayArchives(DataTable data)
        {
            if (data.Rows.Count > 0)
            {
                var dr = data.Rows[0];
                LblName.Text = dr["NAME"].ToString();
                LblIdentityNo.Text = dr["ZJCODE"].ToString();
                LblArchiveNo.Text = dr["DACODE"].ToString();
                
                TxtContent.Text = dr["CONTENT"].ToString();
                TxtRecordTime.Text = dr.IsNull("RECODETIME")
                                         ? string.Empty
                                         : DateTime.Parse(dr["RECODETIME"].ToString()).ToString("yyyy-MM-dd");

                if (DdlArchiveType.Items.FindByValue(dr["XYTYPEID"].ToString()) != null)
                {
                    DdlArchiveType.SelectedValue = dr["XYTYPEID"].ToString();
                }
            }
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            var client = new ServiceClient();
            var data = client.GetDrivingLicence(TxtIdentityNo.Text, TxtArchivesNo.Text);
            TxtWebServiceMsg.Visible = data.WebServiceIsError;
            UpdatePanelCondition.Update();
            if (!data.WebServiceIsError && data.Rows.Count > 0)
            {
                var dataRow = data.Rows[0];
                LblName.Text = dataRow["xm"].ToString();
                LblSex.Text = dataRow["xb"].ToString();
                LblIdentityType.Text = dataRow["sfzmmc"].ToString();
                LblIdentityNo.Text = dataRow["sfzmhm"].ToString();
                LblBorthDay.Text = dataRow["csrq"].ToString();
                LblArchiveNo.Text = dataRow["dabh"].ToString();
                LblFirstLicensing.Text = dataRow["cclzrq"].ToString();
                LblCarType.Text = dataRow["zjcx"].ToString();
                LblDriverCardStatus.Text = dataRow["zt"].ToString();
                LblAuditDate.Text = dataRow["yxqz"].ToString(); //审验日期
                LblLicensingOrgan.Text = dataRow["fzjg"].ToString(); // 发证机关
                LblAdminDivider.Text = dataRow["xzqh"].ToString();
                BtnSave.Enabled = true;
            }
            else
            {
                LblName.Text = string.Empty;
                LblSex.Text = string.Empty;
                LblIdentityType.Text = string.Empty;
                LblIdentityNo.Text = string.Empty;
                LblBorthDay.Text = string.Empty;
                LblArchiveNo.Text = string.Empty;
                LblFirstLicensing.Text = string.Empty;
                LblCarType.Text = string.Empty;
                LblDriverCardStatus.Text = string.Empty;
                LblAuditDate.Text = string.Empty;
                LblManageOrgan.Text = string.Empty;
                LblLicensingOrgan.Text = string.Empty;
                LblAdminDivider.Text = string.Empty;
                BtnSave.Enabled = false;
            }
            UpdatePanelForm.Update();
        }

        protected void BtnSaveClicked(object sender, EventArgs e)
        {
            var id = Request.QueryString["id"];
            DateTime recordDate;
            DateTime.TryParse(TxtRecordTime.Text, out recordDate);

            var db = new ArchivesDb();
            var data = string.IsNullOrEmpty(id) ? db.GetNoneData() : db.GetData(id);
            if (string.IsNullOrEmpty(id))
            {
                var newRow = data.NewRow();
                newRow["XYDATABLE_ID"] = Guid.NewGuid().ToString();
                newRow["ISVALID"] = 1;
                data.Rows.Add(newRow);
            }
            if (data.Rows.Count > 0)
            {
                var dr = data.Rows[0];
                dr["NAME"] = LblName.Text;
                dr["ZJCODE"] = LblIdentityNo.Text;
                dr["DACODE"] = LblArchiveNo.Text;
                dr["XYTYPEID"] = DdlArchiveType.SelectedValue;
                dr["XYTYPENAME"] = DdlArchiveType.SelectedItem.Text;
                dr["CONTENT"] = TxtContent.Text;
                dr["RECODETIME"] = recordDate;
            }
            if (string.IsNullOrEmpty(id))
            {
                db.Add(data);
                var log = new LogInfo(LogType.添加信誉档案)
                              {
                                  Identity = LblIdentityNo.Text,
                                  OwerDriverNo = LblIdentityNo.Text,
                                  OwerArchiveNo = LblArchiveNo.Text,
                                  OwerName = LblName.Text
                              }; // 缺少驾驶证号。
                LogWriter.Writer(log);
            }
            else
            {
                db.Update(data);
                var log = new LogInfo(LogType.修改信誉档案)
                {
                    Identity = LblIdentityNo.Text,
                    OwerDriverNo = LblIdentityNo.Text,
                    OwerArchiveNo = LblArchiveNo.Text,
                    OwerName = LblName.Text
                };// 缺少驾驶证号。
                LogWriter.Writer(log);
            }

            DivSucess.Visible = true;
            UpdatePanelForm.Update();
        }
    }
}